import React from 'react'
import createHistory from 'history/createBrowserHistory'
import {connect} from 'react-redux'
import {loginSuccess, login} from "./redux/user.redux";
import {getStore} from "./tool";
import { Redirect} from 'react-router-dom'
import {getCount} from "./redux/home.redux";
const history = createHistory()
const { location } = history


class AuthRoute extends React.Component{
    componentDidMount() {
        let userinfo = getStore('userinfo')
        if(userinfo){
            this.props.loginDefault(userinfo)
        }
    }
    render(){
        const publicList = ['/login']
        const pathname = location.pathname
        // 是否登录
        let userinfo = getStore('userinfo')
        if(userinfo){
            if (publicList.indexOf(pathname)>-1) {
                location.pathname = '/home'
                return (
                    <Redirect to="/home" />
                )
            }
            return null
        }
        if (publicList.indexOf(pathname)>-1) {
            return null
        }
        return (
            <Redirect to="/login" />
        )
    }
}

const mapDispatchToProps = (dispatch) => ( { // <- forgot the wrapping with ( here
    loginDefault: (data) => {
        dispatch(loginSuccess(data))
    }
} )

function mapStateToProps(state){

    return state || {};
}



// export default App;
export default connect(mapStateToProps,mapDispatchToProps)(AuthRoute);
// export default AuthRoute;
